const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:0,
    hasMoreData:true,
    msg: "正在加载",
    tasks: [],
    inputShowed: false,
    inputVal: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  detail: function (e) {
    var that = this;
    //拿到点击的index下标
    var index = parseInt(e.currentTarget.dataset.index);
    console.log(that.data.tasks[index].id)
    wx.navigateTo({
      url: '../view/view1?zpxxId=' + that.data.tasks[index].id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.showLoading({
      title: that.data.msg
    });
    wx.request({
      url: app.globalData.url + '/zpxx/getZpxx',
      data: {
        page:that.data.page,
        college: app.globalData.studentInfo.collegeName
      },
      success: res => {
        var that = this
        console.log(res.data)
        that.setData({
          tasks: res.data.zpxxList,
          hasMoreData:res.data.count>=that.page
        })
        wx.showToast({
          title: '显示成功'
        });
      },
      fail: function fail(e) {
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 2000 //弹出时间	
        });
      },
      complete: function complete(e) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading();
    that.data.page=0;
    that.setData({
      msg: "正在刷新数据"
    });
    that.onShow();
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasMoreData) {
      that.setData({
        page: that.data.page + 1,
        msg: "加载更多数据"
      });
      that.onShow();
    } else {
      wx.showToast({
        title: '没有更多数据'
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  ,
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  wxSearchConfirm:function(){
    var that = this
    wx.showLoading({
      title: "搜索中"
    });
    wx.request({
      url: app.globalData.url + '/zpxx/searchZpxx',
      data: {
        page: that.data.page,
        college: app.globalData.studentInfo.collegeName,
        searchVal: that.data.inputVal
      },
      success: res => {
        var that = this
        console.log(res.data)
        that.setData({
          tasks: res.data.zpxxList,
          hasMoreData: res.data.count >= that.page
        })
        wx.showToast({
          title: '显示成功'
        });
      },
      fail: function fail(e) {
        wx.showToast({
          title: "获取数据失败",
          icon: 'none',
          duration: 2000 //弹出时间	
        });
      },
      complete: function complete(e) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
      }
    })
  }
})